const { add, update, getAll, getCount } = require('../../utils/dbTools');
const { isValidator,createLog } = require('../../utils/util');

// 角色数据表名称
const tableName = 'role';

/**
 * 获取数据总条数
 * 
*/

const roleTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const result = await getCount(tableName, { remove: 0 });
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 * 获取角色列表数据
*/
const roleList = async (req, res) => {

    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { page, size } = req.query;
    const params = { remove: 0 };
    if (page && size) {
        params.page = page;
        params.size = size;
    }
    const result = await getAll(tableName, params);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
    }
    output.data = result;
    res.send(output);
}

// 新增角色
const roleAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { rolename, permission, status } = req.body;
    if (!rolename || !permission || !status) {
        output.code = 500;
        output.msg = '参数错误';
        return res.send(output);
    }


    // 检测角色名称是否可用
    const valid = await isValidator(tableName, 'rolename', rolename);
    if (!valid) {
        output.code = 500;
        output.msg = '数据已存在,不能重复添加';
        return res.send(output);
    }

    const result = await add({ rolename, permission, status }, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    createLog(req, { type: 10, remark: "新增角色" });

    res.send(output);
}

// 更新角色
const roleUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id, rolename, permission, status } = req.body;
    if (!id || !rolename || !permission || !status) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }
    const data = { rolename, permission, status }
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    createLog(req, { type: 11, remark: "更新角色" });

    res.send(output);
}


// 删除角色
const roleRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    // const result = await remove(id, tableName);
    // 软删除
    const result = await update(id, { remove: 1 }, tableName);

    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    // 记录登录日志
    createLog(req, { type:4, remark: "删除角色" });
    res.send(output);
}

module.exports = {
    roleList,
    roleAdd,
    roleRemove,
    roleUpdate,
    roleTotal
}